/* 
 Codelet from MILEPOST project: http://cTuning.org/project-milepost
 Updated by Grigori Fursin to work with Collective Mind Framework

 3 "./layer3.codelet__6.wrapper.c" 3 4
*/

#include <stdio.h>

int __astex_write_message(const char * format, ...);
int __astex_write_output(const char * format, ...);
void __astex_exit_on_error(const char * msg, int code, const char * additional_msg);
void * __astex_fopen(const char * name, const char * mode);
void * __astex_memalloc(long bytes);
void __astex_close_file(void * file);
void __astex_read_from_file(void * dest, long bytes, void * file);
int __astex_getenv_int(const char * var);
void * __astex_start_measure();
double __astex_stop_measure(void * _before);
typedef signed int  mad_fixed_t;
typedef signed int  mad_fixed64hi_t;
typedef unsigned int  mad_fixed64lo_t;
typedef mad_fixed_t  mad_sample_t;
void  astex_codelet__6(mad_fixed_t xr[576], const mad_fixed_t cs[8], const mad_fixed_t ca[8], const mad_fixed_t *bound);
int main(int argc, const char **argv)
{
  mad_fixed_t  *xr;
  const mad_fixed_t  *cs;
  const mad_fixed_t  *ca;
  const mad_fixed_t  *bound;
  void * codelet_data_file_descriptor = (void *) 0;

#ifdef OPENME
  openme_init(NULL,NULL,NULL,0);
  openme_callback("PROGRAM_START", NULL);
#endif

  if (argc < 2)
    __astex_exit_on_error("Please specify data file in command-line.", 1, argv[0]);
  codelet_data_file_descriptor = __astex_fopen(argv[1], "rb");
  
  char * xr__region_buffer = (char *) __astex_memalloc(4608);
  __astex_write_message("Reading xr value from %s\n", argv[1]);
  __astex_read_from_file(xr__region_buffer, 4608, codelet_data_file_descriptor);
  xr = (mad_fixed_t *) (xr__region_buffer + 0l);
  char * cs__region_buffer = (char *) __astex_memalloc(32);
  __astex_write_message("Reading cs value from %s\n", argv[1]);
  __astex_read_from_file(cs__region_buffer, 32, codelet_data_file_descriptor);
  cs = (const mad_fixed_t *) (cs__region_buffer + 0l);
  char * ca__region_buffer = (char *) __astex_memalloc(32);
  __astex_write_message("Reading ca value from %s\n", argv[1]);
  __astex_read_from_file(ca__region_buffer, 32, codelet_data_file_descriptor);
  ca = (const mad_fixed_t *) (ca__region_buffer + 0l);
  char * bound__region_buffer = (char *) __astex_memalloc(4608);
  __astex_write_message("Reading bound value from %s\n", argv[1]);
  __astex_read_from_file(bound__region_buffer, 4608, codelet_data_file_descriptor);
  bound = (const mad_fixed_t *) (bound__region_buffer + 2304l);
  void * _astex_timeval_before = __astex_start_measure();
  int _astex_wrap_loop = __astex_getenv_int("CT_REPEAT_MAIN");
  if (! _astex_wrap_loop)
    _astex_wrap_loop = 1;

#ifdef OPENME
  openme_callback("KERNEL_START", NULL);
#endif

  while (_astex_wrap_loop > 0)
  {
    --_astex_wrap_loop;
  astex_codelet__6(xr, cs, ca, bound);

  }

#ifdef OPENME
  openme_callback("KERNEL_END", NULL);
#endif

  __astex_write_output("Total time: %lf\n", __astex_stop_measure(_astex_timeval_before));


#ifdef OPENME
  openme_callback("PROGRAM_END", NULL);
#endif

  return 0;
}

